package com.dongjiantong.sys.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.redis.RedisUtil;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.sys.mapper.SysAreaMapper;
import com.dongjiantong.sys.model.SysArea;
import com.dongjiantong.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;

/**
 * Created by cao on 2017/11/9.
 */

@Slf4j
@Service(version = "1.0.0")
public class SysAreaServiceImpl extends ServiceMybatis<SysArea> implements SysAreaService {

    public static final String SysArea_KEY = "SysArea:";

    @Resource
    private SysAreaMapper sysAreaMapper;

    @Override
    public SysArea getAreaName(Integer province_id, Integer city_id, Integer region_id, Integer street_id, Integer village_id)
    {


        SysArea area = new SysArea();
        area.setProvince_id(province_id);
        area.setCity_id(city_id);
        area.setRegion_id(region_id);
        area.setStreet_id(street_id);
        area.setVillage_id(village_id);

        return sysAreaMapper.selectOne(area);
    }

    @Override
    public List<SysArea> getAllList()
    {
        return sysAreaMapper.selectAll();

    }

    @Override
    public List<SysArea> getProvinceList()
    {
        String key = String.format("%sProvinceList", SysArea_KEY);
        List<SysArea> list =  null;//RedisUtil.getList(key);

        if(list!=null)
        {
            return list;
        }

        SysArea area = new SysArea();
        area.setArea_level(1);
        list=  sysAreaMapper.select(area);
        RedisUtil.setList(key,list);
        return list;

    }

    @Override
    public List<SysArea> getCityList(Integer province_id)
    {
        String key = SysArea_KEY+String.format("CityList.%d",province_id);
        List<SysArea> list =  null;//RedisUtil.getList(key);

        if(list!=null)
        {
            return list;
        }

        SysArea area = new SysArea();
        area.setProvince_id(province_id);
        area.setArea_level(2);
        list=   sysAreaMapper.select(area);
        RedisUtil.setList(key,list);
        return list;
    }

    @Override
    public List<SysArea> getRegionList(Integer province_id, Integer city_id)
    {
        String key = SysArea_KEY+String.format("RegionList.%d.%d",province_id,city_id);
        List<SysArea> list =  null;//RedisUtil.getList(key);

        if(list!=null)
        {
            return list;
        }

        SysArea area = new SysArea();
        area.setProvince_id(province_id);
        area.setCity_id(city_id);
        area.setArea_level(3);
        list=   sysAreaMapper.select(area);
        RedisUtil.setList(key,list);
        return list;
    }

    @Override
    public List<SysArea> getStreetList(Integer province_id, Integer city_id, Integer region_id)
    {
        String key = SysArea_KEY+String.format("StreetList.%d.%d.%d",province_id,city_id,region_id);
        List<SysArea> list =  null;//RedisUtil.getList(key);

        if(list!=null)
        {
            return list;
        }

        SysArea area = new SysArea();
        area.setProvince_id(province_id);
        area.setCity_id(city_id);
        area.setRegion_id(region_id);
        area.setArea_level(4);
        list=   sysAreaMapper.select(area);
        RedisUtil.setList(key,list);
        return list;
    }
    @Override
    public List<SysArea> getVillageList(Integer province_id, Integer city_id, Integer region_id, Integer street_id)
    {
        String key = SysArea_KEY+ String.format("VillageList.%d.%d.%d.%d",province_id,city_id,region_id,street_id);
        List<SysArea> list =  null;//RedisUtil.getList(key);

        if(list!=null)
        {

            return list;
        }

        SysArea area = new SysArea();
        area.setProvince_id(province_id);
        area.setCity_id(city_id);
        area.setRegion_id(region_id);
        area.setStreet_id(street_id);
        area.setArea_level(5);
        list=   sysAreaMapper.select(area);
        RedisUtil.setList(key,list);
        return list;
    }
}
